package graphics.app;

import graphics.operations.AddCerchioOperation;
import graphics.operations.AddRettangoloOperation;
import graphics.operations.AddTriangoloOperation;
import graphics.operations.MuoviOperation;
import graphics.operations.ProxyOperation;
import graphics.operations.SelectOperation;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JToolBar;

public class ApplicationFrame extends JFrame{

	private static ApplicationFrame frame;
	public static ProxyOperation proxyOperation;
	
	public static void main(String[] args) {
		frame=new ApplicationFrame();
		frame.setDefaultCloseOperation(EXIT_ON_CLOSE);

		proxyOperation=new ProxyOperation(new AddCerchioOperation());
		proxyOperation.setData(new GraphicsData());
		
		JToolBar bar=new JToolBar();
		
		JButton button01 = new JButton("Cerchio");
		button01.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				proxyOperation.setOperation(new AddCerchioOperation());
			}
		});
		bar.add(button01);

		JButton button02 = new JButton("Rettangolo");
		button02.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				proxyOperation.setOperation(new AddRettangoloOperation());
			}
		});
		bar.add(button02);

		JButton button03 = new JButton("Triangolo");
		button03.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				proxyOperation.setOperation(new AddTriangoloOperation());
			}
		});
		bar.add(button03);

		
		JButton button04 = new JButton("Seleziona");
		button04.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				proxyOperation.setOperation(new SelectOperation());
			}
		});
		bar.add(button04);
		

		JButton button05 = new JButton("Muovi");
		button05.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				proxyOperation.setOperation(new MuoviOperation());
			}
		});
		bar.add(button05);
		
		
		JMenuBar menubar=new JMenuBar();
		
			JMenu file=new JMenu("File");
			menubar.add(file);
			
				JMenuItem open=new JMenuItem("Apri");
					open.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent arg0) {
							GraphicsFiles.open(ApplicationFrame.frame, proxyOperation.getData());
						}
					});
				file.add(open);
				JMenuItem save=new JMenuItem("Salva");
					save.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent arg0) {
							GraphicsFiles.save(ApplicationFrame.frame, proxyOperation.getData());
						}
					});
				file.add(save);
				
		frame.setJMenuBar(menubar);
		ApplicationPanel panel=new ApplicationPanel(proxyOperation);
		
		panel.addMouseListener(proxyOperation);
		panel.addMouseMotionListener(proxyOperation);
		
		frame.getContentPane().add(bar,BorderLayout.NORTH);
		frame.getContentPane().add(panel);
		
		//Errore : aggiunto due volte per sbaglio
//		panel.addMouseListener(proxyOperation);
//		panel.addMouseMotionListener(proxyOperation);
		
		frame.setTitle("Grafica Vettoriale");
		frame.setSize(800,600);
		frame.setVisible(true);
	}
}
